package pyip.lib.javalearn.io.nio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.CharBuffer;

public class BioTcpClient implements Runnable {

    private Socket socket;
    private String host;
    private int port;
    private int timeout;

    public BioTcpClient(String host, int port, int timeout) throws IOException {
        this.host = host;
        this.port = port;
        this.timeout = timeout;
        socket = new Socket();
    }

    @Override
    public void run() {
        System.out.println("TCPClient begin");
        try {
            socket.connect(new InetSocketAddress(host, port), timeout);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        while (true) {
            write();
            read();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void read() {
        BufferedReader reader = null;
        InputStreamReader inReader = null;
        try {
            inReader=new InputStreamReader(socket.getInputStream());
            reader = new BufferedReader(inReader);
            CharBuffer cbuf = CharBuffer.allocate(10240);
            reader.read(cbuf);
            
            cbuf.flip();
            System.out.println("remaining " + cbuf.remaining() + "\t- arrayLength: " + cbuf.array().length + "\t" + cbuf.toString());
            cbuf.clear();
            reader.read(cbuf);
            cbuf.flip();
            System.out.println("remaining " + cbuf.remaining() + "\t- arrayLength: " + cbuf.array().length + "\t" + cbuf.toString());
//            String line = null;
//            while ((line = reader.readLine()) != null) {
//                System.out.println(line);
//            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // if (reader != null) {
            // try {
            // reader.close();
            // } catch (IOException e) {
            // e.printStackTrace();
            // }
            // }
        }
    }

    private void write() {
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(socket.getOutputStream());
            writer.print("h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" +
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    "h2socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInputStream()socket.getInp" + 
                    
                    "utStream()socket.getInputStream()2hello xxx");
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // if (writer != null) {
            // writer.close();
            // }
        }
    }

    public static void main(String[] args) {
        try {
            BioTcpClient client = new BioTcpClient("127.0.0.1", 9999, 60 * 1000);
            new Thread(client).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}